package UILayer;
import ModelLayer.*;
import CtrlLayer.*;
import java.util.*;

public class LeasesUI
{
    LeaseController leaseController;
    public LeasesUI()
    {
        leaseController = new LeaseController();
    }

    public void leasesMenuStart(int sessionId)
    {

        boolean exit=false;
        while (!exit) 
        {
            int choice = writeLeasesMenu(sessionId);

            switch(choice){
                case 1:
                startCreateLease(sessionId);
                break;
                case 2:
                startFindLease();
                break;
                case 3:
                startFinishLease();
                break;
                case 4:
                if(SessionContainer.getInstance().findSessionById(sessionId).getAccessLevel() == 2) {
                    startDeleteLease();
                }
                break;
                case 0:
                exit = true;
                break;
            }

        }
    }

    private int writeLeasesMenu(int sessionId)
    {       

        Scanner keyboard = new Scanner(System.in);
        System.out.println("\f *** Leases Menu ***");
        System.out.println(" (1) Start a lease");
        System.out.println(" (2) Show a lease");
        System.out.println(" (3) Finish a lease");

        if(SessionContainer.getInstance().findSessionById(sessionId).getAccessLevel() == 2 ){
            System.out.println(" (4) Delete a lease"); 
        }
        System.out.println(" (0) Back");
        System.out.print("\n Make your choice: ");
        int choice = keyboard.nextInt();
        return choice;
    }

    private void startCreateLease(int sessionId)
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Enter the information of the new lease:");
        System.out.println("Date:");
        String date = keyboard.nextLine();
        System.out.println("Lease period:");
        String period = keyboard.nextLine();
        System.out.println("Customer ID:");
        int customerID = Integer.parseInt(keyboard.nextLine());

        int leaseId = leaseController.createLease(date,"0",0,period,customerID, sessionId);
        if(leaseId != 0) {
            boolean exit=false;

            while (!exit) 
            {
                System.out.println(" (1) Add Product");
                System.out.println(" (2) Remove Product");
                System.out.println(" (0) End");
                int choice = Integer.parseInt(keyboard.nextLine());

                switch(choice){
                    case 1:
                    addProduct(leaseId);
                    break;
                    case 2:
                    removeProduct(leaseId);
                    break;
                    case 0:
                    exit = true;
                    break;
                }

            }

            System.out.println("Lease is created with id:" + leaseId);
        }else{
            System.out.println("Invalid Customer!");
            leaseController.deleteLease(leaseId);

        }
        pause();
    }

    private void removeProduct(int leaseId){
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Product barcode:");
        int productBarcode = Integer.parseInt(keyboard.nextLine());
        System.out.println("Product amount:");
        int amount = Integer.parseInt(keyboard.nextLine());
        leaseController.removeProductFromLease(leaseId, productBarcode, amount);
    }

    private void addProduct(int leaseId){
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Product barcode:");
        int productBarcode = Integer.parseInt(keyboard.nextLine());
        System.out.println("Product amount:");
        int amount = Integer.parseInt(keyboard.nextLine());
        boolean check = leaseController.addProductInLease(leaseId, productBarcode, amount);
        if(!check) {
            System.out.println("There is no such product!");
            leaseController.removeProductFromLease(leaseId,productBarcode,amount);
        }
    }

    private void startFindLease()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("ID");
        int id = Integer.parseInt(keyboard.nextLine());

        if(leaseController.findLease(id) != null){
            System.out.println();
            System.out.println("ID of lease: " + leaseController.findLease(id).getId());
            System.out.println("Date of lease: " + leaseController.findLease(id).getCurrentDate());
            System.out.println("Period for leasing: " + leaseController.findLease(id).getPeriod());
            System.out.println("-----------Products---------------" );
            for (int key : leaseController.findLease(id).getProducts().keySet()) {
                System.out.println("Product: " + key + "\t \t Amount: " + leaseController.findLease(id).getProducts().get(key));
            }
            System.out.println("-----------------------------------" );
            System.out.println("Employee: " + leaseController.findLease(id).getEmployeeID());
            System.out.println("Customer: " + leaseController.findLease(id).getCustomerID());
            System.out.println("Returned: " + leaseController.findLease(id).isReturned());

        } else {
            System.out.println("The lease has not been found!");
        }
        pause();
    }

    private void startFinishLease()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("ID:");
        int id = Integer.parseInt(keyboard.nextLine());

        Lease lease = leaseController.findLease(id);
        if(lease.onTime())
        {
            System.out.println("The lease has been successfully finished on time!");

        } else {
            System.out.println("You haven`t returned on time, you do not deserve to receive anymore!");
        }
        lease.setReturned(true); 
        pause();
    }

    private void startDeleteLease()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("ID");
        int id = Integer.parseInt(keyboard.nextLine());

        leaseController.deleteLease(id);
    }

    private void pause()
    {
        Scanner keyboard = new Scanner(System.in);
        String vent = keyboard.nextLine();
    }

}
